package com.park.microprofile.configbuilder;

import com.park.microprofile.config.DefaultConfig;
import com.park.microprofile.configsource.ConfigSources;
import com.park.microprofile.converter.Converters;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;


/**
 * 默认构建 {@link Config} 的构建器
 *
 * @author Aaron
 * @date 2021/7/13 14:27
 */
public class DefaultConfigBuilder implements ConfigBuilder {
    /**
     * 配置源集合
     */
    private final ConfigSources configSources;
    /**
     * 类型转换器集合
     */
    private final Converters converters;

    public DefaultConfigBuilder(ClassLoader classLoader) {
        this.configSources = new ConfigSources(classLoader);
        this.converters = new Converters(classLoader);
    }

    @Override
    public ConfigBuilder addDefaultSources() {
        configSources.addDefaultConfigSource();
        return this;
    }

    @Override
    public ConfigBuilder addDiscoveredSources() {
        configSources.addDiscoveredConfigSource();
        return this;
    }

    @Override
    public ConfigBuilder addDiscoveredConverters() {
        converters.discoveredConverters();
        return this;
    }

    @Override
    public ConfigBuilder forClassLoader(ClassLoader loader) {
        configSources.setClassLoader(loader);
        converters.setClassLoader(loader);
        return this;
    }

    @Override
    public ConfigBuilder withSources(ConfigSource... sources) {
        configSources.addConfigSources(sources);
        return this;
    }

    @Override
    public ConfigBuilder withConverters(Converter<?>... converters) {
        this.converters.addConverters(converters);
        return this;
    }

    @Override
    public <T> ConfigBuilder withConverter(Class<T> type, int priority, Converter<T> converter) {
        return this;
    }

    @Override
    public Config build() {
        return new DefaultConfig(configSources, converters);
    }
}
